package com.mstafkumax.ismamazigh;



import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;


public class Setting extends Activity{
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);
		int idiomaSelected=0;
		final Spinner selectLanguage = (Spinner) findViewById(R.id.selectLanguage);
		ArrayAdapter<?> AdLanguage = ArrayAdapter.createFromResource(this, R.array.Languages, android.R.layout.simple_spinner_item);
        AdLanguage.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        selectLanguage.setAdapter(AdLanguage);
        
        idiomaSelected=readPreferences();
        
        selectLanguage.setSelection(idiomaSelected);
        // cuando se selecciona un item del spinner language
        selectLanguage.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
				// TODO Auto-generated method stub
				 addPreferences(selectLanguage.getSelectedItem().toString(),selectLanguage.getSelectedItemPosition());
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	// preferencias
	private int readPreferences(){
		SharedPreferences prefs1 = getSharedPreferences("ficheroconfiguracion", Context.MODE_PRIVATE);    
		//return prefs1.getString("Language",  "English"); 
		return prefs1.getInt("Id",  0);
	}
	private void addPreferences(String idioma, int id){
		SharedPreferences prefs =   getSharedPreferences("ficheroconfiguracion", Context.MODE_PRIVATE);
		   SharedPreferences.Editor editor = prefs.edit();
		   editor.putString("Language", idioma.toString());
		   editor.putInt("Id", id);
		   editor.putString("cod",idioma.toLowerCase().substring(0, 2) );
		   editor.commit();
	}
	/* Mostrar el menu*/
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menusetting, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.about:
            aboutIsmAmazigh();
            return true;
        case R.id.home:
            homeIsmAmazigh();
            return true;
        case R.id.exit:
            exitIsmAmazigh();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
	private void aboutIsmAmazigh() {
		// TODO Auto-generated method stub
		Intent intento = new Intent();
		intento.setComponent(new ComponentName(this,About.class));
		intento.putExtra("About", "About");
		startActivity(intento);		
	}
	private void exitIsmAmazigh() {
		// TODO Auto-generated method stub
		finish();// this method close the aplication
	}
	private void homeIsmAmazigh() {
		// TODO Auto-generated method stub
		Intent intento = new Intent();
		intento.setComponent(new ComponentName(this,IsmAmazighActivity.class));
		intento.putExtra("home", "home");
		startActivity(intento);		
	}


}
